package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDTO;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vo.WmUserVO;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Override
    public ResponseResult findByName(String name) {
        if (StringUtils.isBlank(name)){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser one = getOne(new LambdaQueryWrapper<WmUser>().eq(WmUser::getName, name));
        return ResponseResult.okResult(one);
    }

    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        if (user == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        user.setCreatedTime(new Date());
        save(user);
        return ResponseResult.okResult(user);
    }

    @Override
    @Transactional
    public ResponseResult login(WmUserDTO dto) {
//        参数校验
        if (dto == null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
//        检查用户是否存在
        WmUser wmUser = getOne(new LambdaQueryWrapper<WmUser>().eq(WmUser::getName, dto.getName()));
        if (wmUser == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }
//        检查用户是否可用
        if (!AdminConstants.PASS_AUTH.equals(Short.parseShort(wmUser.getStatus().toString()))){
            CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
        }
//        检查密码
        String password = wmUser.getPassword();
        String salt = wmUser.getSalt();
        if (!MD5Utils.encodeWithSalt(dto.getPassword(),salt).equals(password)){
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
//        更新登录时间，办法token
        wmUser.setLoginTime(new Date());
        updateById(wmUser);
        WmUserVO wmUserVO = new WmUserVO();
        BeanUtils.copyProperties(wmUser,wmUserVO);
        String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",wmUserVO);
        return ResponseResult.okResult(map);
    }
}
